<template>
  <div class="main-layout">
    <el-card class="box-card" shadow="never">
      <el-page-header title="返回" :icon="ArrowLeft" @back="goBack">
        <template #content>
          <span class="text-large font-600 mr-3">通知详情</span>
        </template>
      </el-page-header>

      <div class="news-detail" v-loading="loading">
        <div class="title">
          <h1>{{ noticeData.title }}</h1>
        </div>
        
        <div class="subtitle">
          <p>发布日期：{{ formatDate(noticeData.publish_time || '') }}</p>
        </div>

        <div class="content">
          <div class="text">
            <div v-html="noticeData.content"></div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import NoticeApi from '@/api/notice'
import type { NoticeData } from '@/api/notice/types'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const loading = ref(true)
const noticeData = ref<NoticeData>({
  notice_id: 0,
  title: '',
  content: '',
  notice_file_url: '',
  publish_time: ''
})

// 格式化日期
const formatDate = (dateStr: string) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 获取通知详情
const getNoticeDetail = async (noticeId: string) => {
  try {
    loading.value = true
    const response = await NoticeApi.getNotice(noticeId)
    if (response.data) {
      noticeData.value = response.data || {
        notice_id: 0,
        title: '',
        content: '',
        notice_file_url:'',
        publish_time: ''
      }
    } else {
      throw new Error('获取通知详情失败')
    }
  } catch (error) {
    console.error('获取通知详情失败:', error)
    ElMessage.error('获取通知详情失败')
    router.push('/notice')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  const noticeId = route.params.id as string
  if (noticeId) {
    getNoticeDetail(noticeId)
  } else {
    ElMessage.error('通知ID不存在')
    router.push('/notice')
  }
})
</script>

<style scoped>
.news-detail {
  text-align: left;
  margin: 20px;
  max-width: 800px;
  margin: 20px auto;
}

.title {
  margin: 30px 0 20px;
  text-align: center;
}

.title h1 {
  font-size: 24px;
  color: #333;
  margin: 0;
}

.subtitle {
  text-align: center;
  color: #666;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.content {
  margin-top: 30px;
}

.text {
  font-size: 16px;
  line-height: 1.8;
  color: #333;
}

.notice-file {
  margin-top: 30px;
  text-align: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .news-detail {
    margin: 10px;
  }

  .title h1 {
    font-size: 20px;
  }

  .text {
    font-size: 15px;
  }
}
</style>
  